<template>
  <div class="coins-base">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg">
        <label>币种名称</label>
        <el-input v-model="searchForm.name" size="mini" placeholder="输入币种名称" clearable></el-input>
      </div>
      <!-- <div class="item-msg wallet-item">
        <label>钱包</label>
        <el-select v-model="searchForm.platform_id" placeholder="全部" size="mini" @change="search()">
          <el-option v-for="item in wallets" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>-->
      <div class="item-msg wallet-item">
        <label>平台</label>
        <el-select
          v-model="searchForm.platform"
          placeholder="全部"
          size="mini"
          clearable
          filterable
          @change="search()"
        >
          <el-option v-for="item in platforms" :key="item.id" :value="item.platform"></el-option>
        </el-select>
      </div>
      <div class="item-msg wallet-item">
        <label>主链</label>
        <el-select
          v-model="searchForm.chain"
          placeholder="全部"
          size="mini"
          clearable
          @change="search()"
        >
          <el-option v-for="(item,index) in chains" :key="index" :value="item"></el-option>
        </el-select>
      </div>
      <el-button class="search-btn" @click="search()" size="mini" type="primary">查询</el-button>
      <el-button class="add-btn" size="mini" @click="operateCoin('add')" type="primary">添加币种</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="small"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="id" label="ID" width="120"></el-table-column>
        <el-table-column align="center" prop="name" label="币种名称" width="120"></el-table-column>
        <el-table-column align="center" prop="icon" label="图标">
          <template slot-scope="item">
            <img :src="item.row.icon" class="scaleImg" @click="viewImg(item.row.icon)">
          </template>
        </el-table-column>
        <el-table-column align="center" prop="platform" label="平台"></el-table-column>
        <el-table-column align="center" prop="chain" label="主链"></el-table-column>
        <el-table-column align="center" prop="inviter" label="操作" width="150">
          <template slot-scope="item">
            <el-button type="text" @click="operateCoin('edit',item.row)" class="edit-btn">编辑</el-button>
            <el-button type="text" @click="deleteCoin(item.row)" class="del-btn">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></pages>

      <!-- 查看资产弹窗 -->
      <transition name="fade">
        <img-view :img="imgUrl" v-if="showImg" @cancel="showImg=false"></img-view>
      </transition>
      <!-- 查看添加、编辑弹窗 -->
      <transition name="fade">
        <add-coin
          v-if="showDialog"
          @cancel="showDialog=false"
          @success="showDialog=false;search()"
          :operateData="operateData"
          :platform_id="searchForm.platform_id"
        ></add-coin>
      </transition>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import ImgView from "@/components/Explorer/ImgView.vue";
import AddCoin from "@/components/CoinsManage/AddCoin.vue";
import { getTokenList, deleteToken } from "./Coins.service.js";
export default {
  components: {
    Pages,
    ImgView,
    AddCoin
  },
  computed: {
    wallets() {
      return this.$store.state.wallet_list || [];
    },
    chains() {
      return this.$store.state.main_chains;
    },
    platforms() {
      return this.$store.state.para_list;
    }
  },
  data() {
    return {
      loading: false,
      searchForm: {
        platform_id: String(this.$store.state.userInfo.platform_id),
        name: "",
        platform: "",
        chain: "",
        page: 1,
        size: 10
      },
      tableData: [],
      total: 10,
      imgUrl: "",
      showImg: false,
      showDialog: false,
      operateData: {}
    };
  },

  mounted() {
    this.search();
  },
  methods: {
    async search() {
      this.loading = true;
      const res = await getTokenList(this.searchForm);
      this.tableData = res.data.items || [];
      this.total = res.data.total;
      this.loading = false;
    },
    viewImg(url) {
      this.imgUrl = url;
      this.showImg = true;
    },
    operateCoin(type, data) {
      this.operateData = {
        type: type,
        data: data || {}
      };
      this.showDialog = true;
    },
    async deleteCoin(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${item.name}</span> 币种?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true
        }
      )
        .then(async () => {
          const res = await deleteToken({ id: item.id });
          if (res.code === 0) {
            this.$message.success("删除成功!");
            this.search();
          }
        })
        .catch(() => this.$message.info("已取消删除"));
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search();
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.searchForm.page = 1;
      this.search();
    }
  }
};
</script>

<style lang="scss" scoped>
.coins-base {
  // position: relative;
  min-height: calc(100vh - 100px);
  .search-block {
    width: 100%;
    padding: 25px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .item-msg {
      display: inline-block;
      margin-right: 20px;
      height: 60px;
      line-height: 60px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;
      label {
        display: inline-block;
        max-width: 90px;
        text-align: right;
        margin-right: 10px;
      }
      .el-input {
        width: 150px;
      }
      .el-select {
        width: 150px;
      }
      .el-date-editor {
        width: 270px;
      }
    }
    .wallet-item {
      .el-select {
        width: 130px;
      }
    }
    .add-btn {
      margin-left: 15px;
    }
    .search-btn {
      margin-left: 25px;
    }
  }
  .data-table {
    // position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .scaleImg {
      max-width: 120px;
      max-height: 30px;
      cursor: pointer;
    }
    .del-btn {
      color: #f56c6c;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>